// ---------------------------------------------------------- Ajax AutoComplete --------------------------------------------------------------
// 本控件主要是以html的input和select标签组合为原型，下拉过滤框是select标签,通js控制select的样式来达到模拟过滤效果。
var SaturnAutoCompleteVersion = {Version:"1.0"};
var SaturnAutoComplete = function(autoid,method){
    this.autoid = autoid;
    this.method = method;
};
SaturnAutoComplete.prototype.init =function(queryCondition,funcName){
	var actionStr = eval(funcName);
	while(actionStr.indexOf("'")>=0){
		actionStr = actionStr.replace("'","@");
	}
	while(actionStr.indexOf("%")>=0){
		actionStr = actionStr.replace("%","`");
  	}
	var id=this.autoid;
	var method = this.method;
	var parm = id+":"+method;
	var action = actionStr.split("?")[0];
	var param = actionStr.split("?")[1];
	var str = param+"&queryCondition="+queryCondition;
	var myAjax = new Ajax.SaturnRequest(
			action,{
				method: 'post',
				parameters: str,
				onComplete: this.addAjaxSuggestList,
				args:parm
			}
	);
};
SaturnAutoComplete.prototype.addAjaxSuggestList=function(response,parm){
    var id = parm.split(":")[0];        
    var method = parm.split(":")[1];        
	var codelist = new Array();
	var pylist = new Array();
	var codeAndvaluelist = new Array();
	var xmlDOM = response.responseXML;
	var root = xmlDOM.documentElement;
	var autoid = id;
	if(root.childNodes.length==0){
		clear(autoid);
		error(autoid);
		return;
	}
	var element = root.getElementsByTagName("list").item(0).getElementsByTagName("map"); 
	for(var i=0;i<element.length;i++){
		var valueElement = element[i].getElementsByTagName("value");
		var elecode ;
		var elevalue ;
		var elepy ;
		for(var j=0;j<valueElement.length;j++){
			if(valueElement.item(j).getAttribute("key")=="code"){
				elecode = valueElement.item(j).firstChild.nodeValue;
 	    	}
			if(valueElement.item(j).getAttribute("key")=="value"){
				elevalue = valueElement.item(j).firstChild.nodeValue;
			}
			if(valueElement.item(j).getAttribute("key")=="py"){
				elepy = valueElement.item(j).firstChild.nodeValue;
			}
		}
		codelist.push(elecode);
		pylist.push(elepy);
		codeAndvaluelist.push(elecode+":"+elevalue);
	}
	showtips(autoid,codelist,pylist,codeAndvaluelist,method);	   
};
SaturnAutoComplete.prototype.enterTips=function(){    
	e=event.keyCode;
	if($("sel"+this.autoid).style.display!='none'){
		if(e==13) {
			this.rv(0);
			$("sel"+this.autoid).style.display='none';
			$(this.autoid).focus();
		}
    	if(e==40) {
			$("sel"+this.autoid).focus();
    	}
	}
};
SaturnAutoComplete.prototype.rv=function(flag){
	keyflag = true;
	if(flag==1){
		if($("hidvalue"+this.autoid).value==""&&$("hidcode"+this.autoid).value!=""){
			$("hidvalue"+this.autoid).value = $(this.autoid).value;
		}
		else{
			$(this.autoid).value=$("hidcode"+this.autoid).value;
		}
		var e = event.srcElement;
		var r =e.createTextRange();
		r.moveStart('character',e.value.length);
		r.collapse(true);
		r.select();
	}
	if(flag==0){
		var method = this.method;
		$(this.autoid).value=$("sel"+this.autoid).value.split(":")[1];
		$("hidvalue"+this.autoid).value=$("sel"+this.autoid).value.split(":")[1];
		$("hidcode"+this.autoid).value=$("sel"+this.autoid).value.split(":")[0];
		eval(method);
		$(this.autoid).focus();
		this.c();
	} 
};
SaturnAutoComplete.prototype.onblurEvent=function(){
	if($(this.autoid).value==""){
		$(this.autoid).value="";
		$("hidvalue"+this.autoid).value="";
		$("hidcode"+this.autoid).value="";
	}
	if($("hidvalue"+this.autoid).value==""&&$("hidcode"+this.autoid).value!=""){
		$("hidvalue"+this.autoid).value = $(this.autoid).value;
	}
	else{
		$(this.autoid).value=$("hidvalue"+this.autoid).value;
	}
	keyflag = true;
};
SaturnAutoComplete.prototype.c=function(){
	$("sel"+this.autoid).style.display='none';
};
var selvalue;
function showtips(id,codelist,pylist,codeAndvaluelist,method){
	$(id).style.color="";      
	$("sel"+id).length=0;
	var len=pylist.length;
	var re=new RegExp("^"+$F(id),"i")
	var j=0
	for(i=0;i<len;i++) 
	if(re.test(codeAndvaluelist[i])==true||re.test(pylist[i])==true||re.test(codeAndvaluelist[i].split(":")[1])==true){
		$("sel"+id).style.display='';
		$("sel"+id).add(new Option(codeAndvaluelist[i],codeAndvaluelist[i]));
		j++;
	} 
	$("sel"+id).size = (j>1)?j:2;
	if($("sel"+id).options[0]){
		$("sel"+id).options[0].selected = true;
	}
	keyflag = false;
	if(codelist.length==1&&selvalue!=$("sel"+id).options(0).value.split(":")[1]){
		selvalue = $("sel"+id).options(0).value.split(":")[1];
		$(id).value=$("sel"+id).options(0).value.split(":")[1]; 
		$("hidvalue"+id).value=$("sel"+id).value.split(":")[1];
		$("hidcode"+id).value=$("sel"+id).value.split(":")[0];
		$("sel"+id).style.display='none';     
		eval(method);
	}
	if(codelist.length!=1){
		selvalue=null;
	}
}
function error(id){
	document.getElementById(id).style.color="red";   
}
function ajaxShowDialog(id,pars,method){
	var retval = window.showModalDialog(rootPath()+"/demo/dialog.jsp?"+pars,window,"dialogHeight:350px;dialogWidth:430px;help:no;status:no;scroll:0;center:yes");   
	$(id).style.color="";
	if(retval!=null){
		$(id).value=retval.split(":")[1];
		$("hidcode"+id).value=retval.split(":")[0];
		$("hidvalue"+id).value=retval.split(":")[1];
		$(id).focus();
		keyflag = true;
		eval(method);
	}
}
function clear(id){
	$("sel"+id).style.display='none';
	$("hidvalue"+id).value='';
	$("hidcode"+id).value='';
}
document.onclick=function(){
	for(var i=0;i<document.all.length;i++){
		if(document.all[i].tagName =="SELECT"){
		var str = document.all[i].id;
		if(str.startWith("sel")){
			document.all[i].style.display='none';
		}
	  }
	}
}
String.prototype.startWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
		return false;
		if(this.substr(0,str.length)==str){
		return true;
	}
	else
	return false;
	return true;
}